<template>
  <el-button size="small" type="primary" @click="handleAdd()">
    新增
  </el-button>
  <el-card>

    <!--查询条件-->
    <el-form :inline="true" :model="condition" class="demo-form-inline">
      <el-form-item label="路径名称：">
        <el-input v-model="condition.name" placeholder="请输入名称" clearable />
      </el-form-item>
      <el-form-item label="是否推荐：">
        <el-select v-model="condition.is_recommended" placeholder="请选择" clearable>
          <el-option label="推荐" value="1" />
          <el-option label="不推荐" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchPath()">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="路径ID" width="80" />
      <el-table-column prop="name" label="路径名称" />
      <el-table-column prop="difficulty" label="难度" width="100" />
      <el-table-column prop="days" label="总天数" width="80" />
      <el-table-column prop="user_id" label="用户ID" width="100" />
      <el-table-column prop="sort" label="排序" width="80" />
      <el-table-column prop="is_recommended" label="是否推荐" width="100">
        <template #default="scope">
          {{ scope.row.is_recommended === 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" width="160" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleView(scope.row)">
            查看
          </el-button>
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="您确定要删除吗?" @confirm="handleRemove(scope.row.id)">
            <template #reference>
              <el-button size="small" type="warning">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="demo-pagination-block">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[6, 7, 8, 9]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <!-- 查看对话框 -->
    <el-dialog v-model="viewDialogVisible" title="路径详情" width="50%">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="路径ID">{{ currentPath.id }}</el-descriptions-item>
        <el-descriptions-item label="路径名称">{{ currentPath.name }}</el-descriptions-item>
        <el-descriptions-item label="路径描述">{{ currentPath.description }}</el-descriptions-item>
        <el-descriptions-item label="难度">{{ currentPath.difficulty }}</el-descriptions-item>
        <el-descriptions-item label="总天数">{{ currentPath.days }}</el-descriptions-item>
        <el-descriptions-item label="包含任务">{{ currentPath.tasks }}</el-descriptions-item>
        <el-descriptions-item label="缩略图">
          <img v-if="currentPath.thumbnail_url" :src="currentPath.thumbnail_url" style="max-width: 200px" />
          <span v-else>无图片</span>
        </el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ currentPath.user_id }}</el-descriptions-item>
        <el-descriptions-item label="排序">{{ currentPath.sort }}</el-descriptions-item>
        <el-descriptions-item label="是否推荐">{{ currentPath.is_recommended === 1 ? '推荐' : '不推荐' }}</el-descriptions-item>
        <el-descriptions-item label="用户笔记">{{ currentPath.user_note || '无' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ currentPath.created_at }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ currentPath.updated_at }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 编辑对话框 -->
    <el-dialog v-model="editDialogVisible" :title="editDialogTitle" width="50%">
      <el-form :model="currentPath" label-width="80px">
        <el-form-item label="路径ID">
          <el-input v-model="currentPath.id" disabled />
        </el-form-item>
        <el-form-item label="路径名称" required>
          <el-input v-model="currentPath.name" />
        </el-form-item>
        <el-form-item label="路径描述">
          <el-input v-model="currentPath.description" type="textarea" />
        </el-form-item>
        <el-form-item label="难度" required>
          <el-input v-model="currentPath.difficulty" />
        </el-form-item>
        <el-form-item label="总天数" required>
          <el-input v-model="currentPath.days" type="number" />
        </el-form-item>
        <el-form-item label="包含任务">
          <el-input v-model="currentPath.tasks" type="textarea" />
        </el-form-item>
        <el-form-item label="缩略图URL">
          <el-input v-model="currentPath.thumbnail_url" />
        </el-form-item>
        <el-form-item label="用户ID" required>
          <el-input v-model="currentPath.user_id" type="number" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="currentPath.sort" type="number" />
        </el-form-item>
        <el-form-item label="是否推荐">
          <el-select v-model="currentPath.is_recommended" placeholder="请选择">
            <el-option label="推荐" value="1" />
            <el-option label="不推荐" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户笔记">
          <el-input v-model="currentPath.user_note" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axiosObject from '@/main.js';

const tableData = ref([])
const viewDialogVisible = ref(false)
const editDialogTitle = ref("")
const editDialogVisible = ref(false)
const currentPath = ref({})

//查询条件
const condition = ref({ "name": "", "difficulty": "", "user_id": "", "is_recommended": "" })

//分页元素
const currentPage = ref(1)
const pageSize = ref(8)
const total = ref(0)

//页面加载完成后自动查询数据
onMounted(() => {
  findPathList();
})

//分页函数：handleCurrentChange
const handleCurrentChange = () => {
  findPathList();
}

//分页的函数：handleSizeChange，当pageSize发生改变时调用此函数
const handleSizeChange = () => {
  currentPage.value = 1; // 切换页大小时重置到第一页
  findPathList();
}

//带条件查询
const searchPath = () => {
  currentPage.value = 1;
  findPathList();
}

const handleView = (row) => {
  currentPath.value = { ...row }
  viewDialogVisible.value = true
}

const handleAdd = () => {
  editDialogTitle.value = "添加学习路径";
  currentPath.value = {
    days: 1,
    sort: 0,
    is_recommended: 0,
    user_id: 0
  };
  editDialogVisible.value = true;
}

//编辑界面
const handleEdit = (row) => {
  editDialogTitle.value = "编辑学习路径";
  currentPath.value = { ...row }
  editDialogVisible.value = true
}

//根据id删除
const handleRemove = async (id) => {
  try {
    await axiosObject.delete("learningPath/" + id);
    await findPathList();
  } catch (error) {
    console.error("删除失败:", error);
  }
}

const handleSave = async () => {
  try {
    //如果currentPath.value.id有值
    if (currentPath.value.id) {
      //修改，ajax引擎对象向后端发起put请求
      await axiosObject.put("learningPath", currentPath.value);
    } else {
      //新增，ajax引擎对象向后端发起post请求
      await axiosObject.post("learningPath", currentPath.value);
    }

    await findPathList();
    editDialogVisible.value = false
  } catch (error) {
    console.error("保存失败:", error);
  }
}

const findPathList = async () => {
  const payload = {
    ...condition.value,
    page: currentPage.value,
    size: pageSize.value
  };

  try {
    const result = await axiosObject.post('/learningPath/list', payload);
    console.log(result.data);

    tableData.value = result.data.resultData.list || [];
    total.value = result.data.resultData.total || 0;
  } catch (error) {
    console.error("获取学习路径列表失败:", error);
    tableData.value = [];
    total.value = 0;
  }
};
</script>

<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}

.demo-pagination-block {
  margin-top: 15px;
  text-align: right;
}
</style>